<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class News extends MY_Controller {

    public function __construct(){
        parent::__construct();
        $this->load->model('news_mdl');
    }

    public function index(){
        //分页
        $url = '';
        $this->db->start_cache();
		 $this->db->where('user_id',$_SESSION['user']['id']);
		 $this->db->where('status !=','3');
		 
		 $this->db->stop_cache();

        $this->load->library('pagination');
        $this->config->load('page', TRUE);
        $config = $this->config->config;
        $config = $config['page'];
        $config['base_url'] = site_url('user/index').$url;
        $config['total_rows'] = $this->news_mdl->get_count();

        $this->pagination->initialize($config);
        $data['page'] = $this->pagination->create_links();

        $per_page = $this->input->get('per_page');
        $this->db->limit($config['per_page'],intval($per_page));
        $this->db->order_by('id desc');
        $list = $this->news_mdl->select();
        $data['rows'] = $config['total_rows'];
        //echo $this->db->last_query();
        $this->db->flush_cache();
        $type = $this->news_mdl->_type;

        foreach($list as $k => $v){
            $list[$k]['type_name'] = $type[$v['type']];
        }

        $data['list'] = $list;
        $this->_view('index',$data);

    }

    /**
     * 更改信息状态
     */
    public function set_status(){
        $nid = $this->input->get('id');
        $this->db->where('id',$nid);
        $this->db->where('user_id',$_SESSION['user']['id']);
        $this->db->set('status','2');
        $res = $this->news_mdl->update();
        echo $res == true ? '1' : '后台数据错误！';
    }

    /**
     * 删除信息
     */
    public function del_news(){
        $nid = $this->input->post('nid');
        $this->db->where('id',intval($nid));
        $this->db->where('user_id',$_SESSION['user']['id']);
		 $this->db->set('status',3);
        $res = $this->news_mdl->update();
        //echo $this->db->last_query();
        echo $res == true ? '1' : '后台数据错误！';
    }

    public function get_news(){
        if(!$this->input->is_ajax_request()){
            show_404();
        }

        echo $this->news_mdl->get_news();
    }

}

/* End of file News.php */
/* Location: ./application/controllers/News.php */